#coding=utf-8
import jsonpath

__author__ = 'Meteor'

import requests
from tools.loggingModule import logger
from requests.exceptions import MissingSchema
import os
import seting



class Reque(object):


    def __init__(self):

        self.response =None
        self.session = requests.Session()


    def send_request(self,datas):
        self.data = datas

        data = None
        url = None
        method = None
        params = None
        json = None
        # 保存提取后的数据
        self.ext_data = {}





        if 'url' not in self.data:
            logger.error('url error')
            raise MissingSchema('URL error')
        else:
            # 拼接URL地址
            url = seting.BASEURL + self.data.get('url')
            logger.info('get url %s' %url)


        if 'headers' in self.data:
            headers = self.data.get("headers")
            for k,v in headers.items():
                if v.startswith('$'):
                    headers[k] = os.environ[(v.lstrip('$'))]

            logger.info('Headers : %s' %str(headers))

        else:
            headers = {}



        if 'data' in self.data:
            data = self.data.get('data')
            logger.info('get data %s' %data)
        if 'params'  in self.data:
            params = self.data.get('params')
            logger.info('get params %s' %params)
        if 'json' in self.data:
            json = self.data.get('json')
        if 'method' not in self.data:
            method = "get"
            logger.info('not fond method,default set  %s' %method)
        else:
            method = self.data.get("method")
            logger.info('get method %s' %method)

        # 判断参数是data、params、json
        if  data != None and params != None:
            self.response = self.session.request(method=method,url=url,data=data,params=params)
            logger.info('send request method: %s,url:%s,data:%s,params:%s' %(method,url,data,params))
        elif data != None:
            logger.info('send request method: %s,url:%s,data:%s' %(method,url,data))
            self.response = self.session.request(method=method,url=url,data=data)

        elif json != None and params != None:
            self.response = self.session.request(method=method,url=url,json=data,params=params)
            logger.info('send request method: %s,url:%s,json:%s,params:%s' %(method,url,json,params))
        elif json != None:
            self.response = self.session.request(method=method,url=url,json=data)
            logger.info('send request method: %s,url:%s,json:%s' %(method,url,json))
        elif params != None:

            self.response = self.session.request(method=method,url=url,params=params,headers=headers)
            logger.info('send request method: %s,url:%s,params:%s' %(method,url,params))

        self.extra()
        return self.response

    def get_response(self):
        return self.response

    def extractor(self,extractors):
        for k,v in extractors.items():
            # dict_ext[k] = v
            os.environ[k] = jsonpath.jsonpath(self.response.json(),v)[0]







        # 从响应报文中获取token
        # token = jsonpath.jsonpath(self.response.json(),'$..token')
        # logger.info('TOKEN:%s' %token)

    def extra(self):
        if 'extractor' in self.data:

            self.extractor(self.data.get('extractor'))


